package wiki.xsx.service;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Descript
 * @Author xsx
 * @Date 2017/11/20
 */
public class MySQLService extends AbstractService{
    @Override
    public String queryTableDefine(String tableName){
        try{
            DatabaseMetaData metaData = this.connection.getMetaData();
            ResultSet result = metaData.getColumns(this.database.getDbName(), this.database.getDbUserName().toUpperCase(), tableName, "%");
            
            while (result.next()){
                System.out.println("***********************************************************");
                System.out.println("TABLE_CAT :"+result.getString("TABLE_CAT"));
                System.out.println("TABLE_SCHEM :"+result.getString("TABLE_SCHEM"));
                System.out.println("TABLE_NAME :"+result.getString("TABLE_NAME"));
                System.out.println("COLUMN_NAME :"+result.getString("COLUMN_NAME"));
                System.out.println("DATA_TYPE :"+result.getString("DATA_TYPE"));
                System.out.println("TYPE_NAME :"+result.getString("TYPE_NAME"));
                System.out.println("COLUMN_SIZE :"+result.getString("COLUMN_SIZE"));
                System.out.println("DECIMAL_DIGITS :"+result.getString("DECIMAL_DIGITS"));
                System.out.println("NUM_PREC_RADIX :"+result.getString("NUM_PREC_RADIX"));
                System.out.println("NULLABLE :"+result.getString("NULLABLE"));
                System.out.println("REMARKS :"+result.getString("REMARKS"));
                System.out.println("COLUMN_DEF :"+result.getString("COLUMN_DEF"));
                System.out.println("CHAR_OCTET_LENGTH :"+result.getString("CHAR_OCTET_LENGTH"));
                System.out.println("ORDINAL_POSITION :"+result.getString("ORDINAL_POSITION"));
                System.out.println("IS_NULLABLE :"+result.getString("IS_NULLABLE"));
                System.out.println("IS_AUTOINCREMENT :"+result.getString("IS_AUTOINCREMENT"));
                System.out.println("***********************************************************");
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        return null;
//        StringBuilder sql = new StringBuilder();
//        sql.append("SELECT * FROM ")
//            .append("`").append(this.database.getDbName()).append("`.").append(tableName)
//            .append(" LIMIT 0,1");
//        try(
//            PreparedStatement statement = this.connection.prepareStatement(sql.toString());
//            ResultSet result = statement.executeQuery()
//        ){
//            ResultSetMetaData metaData = result.getMetaData();
//            return this.formatTableDefine(result.getString("Create Table"));
//        }catch (SQLException e){
//            throw new RuntimeException(e);
//        }
    }

    @Override
    public String formatTableDefine(String tableDefine){
        StringBuilder newTableDefine = new StringBuilder();
        if (tableDefine.contains("CONSTRAINT")){
            newTableDefine.append(tableDefine.substring(0, tableDefine.indexOf("CONSTRAINT")-4)).append("\n)") ;
        }else{
            newTableDefine.append(tableDefine.substring(0, tableDefine.indexOf("ENGINE")));
        }
        return newTableDefine.toString();
    }
}
